<template>
  <div>
    <!-- 三级分类列表 -->
    <el-card style="margin: 20px 0px">
      <CategoryList @getCategoryList="getCategoryList" :show="scene == 0" />
    </el-card>
    <el-card>
      <!-- SPU属性展示栏 -->
      <div v-show="scene == 0">
        <!-- 添加属性按钮 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="AddSpu"
          :disabled="addBtnShow"
        >
          添加SPU
        </el-button>
        <!-- SPU属性列表 -->
        <el-table style="width: 100%" border :data="records">
          <el-table-column prop="prop" label="序号" type="index" width="80">
          </el-table-column>
          <el-table-column
            prop="spuName"
            label="SPU名称"
            width="width"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <hint-Button
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加SPU"
                @click="addSku(row)"
              ></hint-Button>
              <hint-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改SPU"
                @click="updateSpu(row)"
              ></hint-button>
              <hint-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="SPU详情信息"
                @click="ShowSkuInfoList(row)"
              ></hint-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
                <hint-button
                slot="reference"
                type="danger"
                icon="el-icon-delete"
                size="mini"
                title="删除SPU"
              ></hint-button> 
              </el-popconfirm>
              </template
            >``
          </el-table-column>
        </el-table>
        <!-- 分页器-->
        <el-pagination
          @current-change="getSPUList"
          @size-change="handleSizeChange"
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev, pager, next, jumper,->,total, sizes"
          :total="total"
        >
        </el-pagination>
      </div>
      <!-- 添加或修改SPU栏 -->
      <SpuForm
        v-show="scene == 1"
        @ChangeScene="ChangeScene"
        ref="spu"
      ></SpuForm>
      <!-- 添加SkuForm -->
      <SkuForm v-show="scene == 2" ref="sku" @ChangeScene="ChangeScenes"></SkuForm>
    </el-card>
    <!-- Sku列表信息对话框 -->
    <el-dialog :title="`${spu.spuName}列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <el-table :data="SkuList" border v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width"></el-table-column>
        <el-table-column prop="price" label="价格" width="width"></el-table-column>
        <el-table-column prop="weight" label="重量" width="width"></el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{row,$index}">
            <img :src="row.skuDefaultImg" style="width:100px;height:100px;"></img>
          </template>
        </el-table-column>

      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SkuForm from "./SkuForm";
import SpuForm from "./SpuForm";
export default {
  name: "Spu",
  props: {},
  components: {
    SkuForm,
    SpuForm,
  },
  data() {
    return {
      // 三级分类列表
      category1Id: "",
      category2Id: "",
      category3Id: "",
      // 当前页数
      page: 1,
      // 单页最大显示数目
      limit: 3,
      // SPU商品总记录数
      total: 0,
      // SPU属性列表
      records: [],
      // SPU属性栏展示场景值,0为默认值,1为SpuForm组件,2为SkuForm组件
      scene: 0,
      // 添加属性按钮是否可用
      addBtnShow: true,
      // 是否显示对话框
      dialogTableVisible:false,
      // 存储选中的spu
      spu:{},
      // Sku列表信息
      SkuList:[],
      // 是否加载
      loading:true
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 获得三级列表分类ID
    getCategoryList({ categoryId, level }) {
      // 判断分类级数
      if (level === 1) {
        // 清除数据
        this.category2Id = "";
        this.category3Id = "";
        // 获得分类id
        this.category1Id = categoryId;
      } else if (level === 2) {
        // 清除数据
        this.category3Id = "";
        // 获得分类id
        this.category2Id = categoryId;
      } else if (level == 3) {
        this.category3Id = categoryId;
        // 获得商品属性
        this.getSPUList();
        this.addBtnShow = false;
      }
    },
    // 获得SPU属性列表
    async getSPUList(pages = 1) {
      this.page = pages;
      const { page, limit, category3Id } = this;
      const res = await this.$API.spu.reqSPUList(page, limit, category3Id);
      // console.log(res);
      if (res.code == 200) {
        this.records = res.data.records;
        this.total = res.data.total;
      }
    },
    // 改变单页显示最大数目
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSPUList();
    },
    // 添加Spu属性回调
    AddSpu() {
      this.scene = 1;
      this.$refs.spu.addSpuData(this.category3Id);
    },
    // 修改Spu属性回调
    updateSpu(row) {
      this.scene = 1;
      console.log(this.$refs.spu.InitSpuData(row));
    },
    // 改变场景值自定义事件
    ChangeScene({ scene, flag }) {
      // 改变场景值后需要重新更新页面并跳回第一页
      this.scene = scene;
      // 如果是修改则跳回当前页，如果是添加则返回第一页
      if (flag == "修改") {
        this.getSPUList(this.page);
      } else {
        this.getSPUList();
      }
    },
    // 删除Spu功能
    async deleteSpu(row){
      const res=await this.$API.spu.reqDeleteSpu(row.id)
      if(res.code==200){
        // 成功则重新发起获取SPU列表请求
        this.$message({
          type:"success",
          message:"删除成功"
        })
        this.getSPUList();
      }
    },
    // 进入添加Sku组件
    addSku(row){
      this.scene=2
      this.$refs.sku.getSkuData(this.category1Id,this.category2Id,row)
    },
    // 改变场景值
    ChangeScenes(scene){
      this.scene=scene
    },
    async ShowSkuInfoList(row){
      // 显示对话框
      this.dialogTableVisible=true
      this.spu=row
      const result=await this.$API.sku.reqSkuList(this.spu.id)
      // console.log(result);
      if(result.code==200){
        this.SkuList=result.data
        this.loading=false
      }
    },
    close(done){
      this.loading=true
      this.SkuList=[]
      done()
    }
  },
};
</script>

<style scoped lang="less"></style>
